package ua.kpi.javatraining.university;

/**
 * @author Vasily Yatsenko
 */
public class AcademicGroup extends Department implements LimitedGroup {
    private MiniCollection<Student> students;
    private final LimitedUniversity university;

    public AcademicGroup(String name, String headmaster, LimitedUniversity university) {
        super(name, headmaster);
        this.university = university;
        this.students = new MiniArrayList<Student>();
    }

    public LimitedUniversity getUniversity() {
        return (LimitedUniversity) university;
    }

    public boolean isEmpty() {
        return students.isEmpty();
    }

    public void addStudent(Student student) {
        student.setGroup((LimitedGroup) this);
        students.add(student);
    }

    public Student getStudent(int index) {
        return students.get(index);
    }

    public void removeStudent(int index) {
        students.remove(index);
    }

    public void removeAllStudents() {
        students.clear();
    }

    public String getStudentsList() {
        StringBuilder sb = new StringBuilder("Students of ");

        sb.append(this.getName());
        sb.append("\n");
        for (Object student : students.toArray()) {
            sb.append((Student) student);
            sb.append("\n");
        }
        return sb.toString();
    }

    public String getCurriculum() {
        return getUniversity().getCurriculum(this);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if ((obj == null) || (obj.getClass() != this.getClass())) {
            return false;
        }
        AcademicGroup other = (AcademicGroup) obj;

        return this.getName().equals(other.getName());
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("Group ");

        builder.append(super.toString());
        return builder.toString();
    }
}
